/**
 * 作者: chenlingzhi
 * 日期: 2024-12-23 21:42:36
 * 备注: 用户特权卡信息
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserPrivilegeCard;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserPrivilegeCardModel extends BaseModel implements Model {
	public static final String LISTKEY = "user_id.type";
	public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
	public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
	public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

	protected long id; // id
	protected long userId; // 玩家id
	protected int type; // 特权卡
	protected long endTime; // 到期时间 (0:永不过期)
	protected long dailyRewardTime; // 每日奖励是否领取 (时间为当天已领取)

	public void reset() {
	}

	protected String getKey() {
		return "UserPrivilegeCard." + userId + "." + type;
	}

	protected String getListKey() {
		return "UserPrivilegeCard." + userId;
	}

	protected String getListItemKey() {
		return type + "";
	}

	public void doCreate() {
		setTableId();
		insert();
	}

	public void setTableId() {
		putId(incrTableId("user_privilege_card"));
	}

	public JsonObject insert() {
		return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
	}

	public void delete() {
		super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
	}

	/**
	 * 根据入库类型更新
	 */
	public JsonObject update() {
		return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
	}

	/**
	 * 实时入队列
	 */
	public JsonObject updateQueueNow() {
		return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
	}

	public JsonObject getAllObj(){
		JsonObject obj = new JsonObject();
		obj.put("id", id); // id
		obj.put("userId", userId); // 玩家id
		obj.put("type", type); // 特权卡
		obj.put("endTime", endTime); // 到期时间 (0:永不过期)
		obj.put("dailyRewardTime", dailyRewardTime); // 每日奖励是否领取 (时间为当天已领取)
		return obj;
	}

	public PBUserPrivilegeCard.Builder takeInitBuilder() {
		PBUserPrivilegeCard.Builder sb = PBUserPrivilegeCard.newBuilder();
		if (type != 0) {
			sb.setType(type);
		}
		if (endTime != 0) {
			sb.setEndTime(endTime);
		}
		if (dailyRewardTime != 0) {
			sb.setDailyRewardTime(dailyRewardTime);
		}
		return sb;
	}

	public PBUserPrivilegeCard.Builder takeBuilder() {
		if (updateObj == null || updateObj.isEmpty()) {
			return takeInitBuilder();
		}
		PBUserPrivilegeCard.Builder sb = PBUserPrivilegeCard.newBuilder();
		if (isFieldUpdate("type")) {
			sb.setType(type);
		}
		if (isFieldUpdate("endTime")) {
			sb.setEndTime(endTime);
		}
		if (isFieldUpdate("dailyRewardTime")) {
			sb.setDailyRewardTime(dailyRewardTime);
		}
		return sb;
	}

	public long getId() {
		return id;
	}

	public void putId(long id) {
		if (this.id != id) {
			this.id = id;
			updateRedisHashField(getKey(), "id", this.id);
		}
	}

	public void addId(long value) {
		this.id += value;
		updateRedisHashField(getKey(), "id", this.id);
	}

	public long getUserId() {
		return userId;
	}

	public void putUserId(long userId) {
		if (this.userId != userId) {
			this.userId = userId;
			updateRedisHashField(getKey(), "userId", this.userId);
		}
	}

	public void addUserId(long value) {
		this.userId += value;
		updateRedisHashField(getKey(), "userId", this.userId);
	}

	public int getType() {
		return type;
	}

	public void putType(int type) {
		if (this.type != type) {
			this.type = type;
			updateRedisHashField(getKey(), "type", this.type);
		}
	}

	public void addType(int value) {
		this.type += value;
		updateRedisHashField(getKey(), "type", this.type);
	}

	public long getEndTime() {
		return endTime;
	}

	public void putEndTime(long endTime) {
		if (this.endTime != endTime) {
			this.endTime = endTime;
			updateRedisHashField(getKey(), "endTime", this.endTime);
		}
	}

	public void addEndTime(long value) {
		this.endTime += value;
		updateRedisHashField(getKey(), "endTime", this.endTime);
	}

	public long getDailyRewardTime() {
		return dailyRewardTime;
	}

	public void putDailyRewardTime(long dailyRewardTime) {
		if (this.dailyRewardTime != dailyRewardTime) {
			this.dailyRewardTime = dailyRewardTime;
			updateRedisHashField(getKey(), "dailyRewardTime", this.dailyRewardTime);
		}
	}

	public void addDailyRewardTime(long value) {
		this.dailyRewardTime += value;
		updateRedisHashField(getKey(), "dailyRewardTime", this.dailyRewardTime);
	}

}